package com.coocaa.internship.controller;


import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.dao.UserLoginDao;
import com.coocaa.internship.pojo.UserLogin;
import com.coocaa.internship.service.serviceImpl.AuthServicelmpl;
import com.coocaa.internship.service.serviceImpl.UserLoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;


/**
 * @Description:
 * @Author: 凌贤
 * @Date: 2019-07-29
 * @Time: 18:47
 */
@SuppressWarnings("unchecked")
@RestController
public class UserLoginController {
    @Autowired
    UserLoginServiceImpl userLoginService;
    @Autowired
    AuthServicelmpl authServicelmpl;
    @Autowired
    UserLoginDao userLoginDao;
    @PostMapping("/LoginByValidateCode")
    public ResultData Login(HttpServletResponse response, UserLogin userLogin, String code) {
        ResultData resultData = new ResultData();
        if (userLoginService.LoginByValidateCode(userLogin, Integer.valueOf(code))) {
            userLogin = userLoginDao.selectUserLoginByPhone(userLogin);
            String token = authServicelmpl.creatToken(userLogin);
            Cookie token1 = new Cookie("token", token);
            Cookie userId = new Cookie("userId", String.valueOf(userLogin.getUserId()));
            response.addCookie(token1);
            response.addCookie(userId);
            resultData.setData(userLogin);
            resultData.setCode(ResultCode.SUCCESS);
        } else {
            resultData.setCode(ResultCode.TOKEN_FAILED);
        }
        return resultData;
    }
}